package com.fansea.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.db.Page;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fansea.dao.BlackGroupDao;
import com.fansea.domain.BlackGroup;
import com.fansea.util.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@RestController
@Slf4j
public class QqRobotController {

    @Value("${user.wxid}")
    public String adminUserWxid;

    //允许发送的群聊
    //我发送可以关闭
    private Boolean flag = true;
    private ArrayList<String> groupList = new ArrayList<>();

    @Resource
    private BlackGroupDao blackGroupDao ;

    @GetMapping("/abc")
    public Object QqRobotEven(String message) {
        BlackGroup blackGroup = new BlackGroup();
        blackGroup.setNotice_id("1");
        blackGroup.setCreateTime(new Date());
        BlackGroup save = blackGroupDao.save(blackGroup);

        PageRequest pageRequest = PageRequest.of(0, 10);
        Iterable<BlackGroup> all = blackGroupDao.findAll(pageRequest);
        return all;
    }

    @GetMapping("/delMsg")
    public String delMsg(String msg, String sender, String self, String roomsender) {


        if (!StringUtils.isEmpty(roomsender) && roomsender.equals(adminUserWxid) && msg.contains("本群退下")) {

            SendMsg.send(sender, "小y：为您关闭本群发言");
            groupList.add(sender);
            return "";
        }
        if (!StringUtils.isEmpty(roomsender) && roomsender.equals(adminUserWxid) && msg.contains("本群恢复")) {

            SendMsg.send(sender, "小y：为您开启本群发言");
            groupList.remove(sender);
            return "";
        }

        if (groupList.contains(sender))
            return "";

        if (!StringUtils.isEmpty(roomsender) && roomsender.equals(adminUserWxid) && msg.equals("退下")) {
            flag = false;
            SendMsg.send(sender, "小y：为您关闭");
            if (msg.contains("退下并且")) {

                msg = msg.replace("退下并且关闭", "");
                SendMsg.send(sender, "小y：调用小爱同学API为您关闭" + msg);

            }
            return "";
        }

        if (!StringUtils.isEmpty(roomsender) && roomsender.equals(adminUserWxid) && msg.equals("恢复")) {
            flag = true;
            SendMsg.send(sender, "小y：为您恢复");
            if (msg.contains("恢复并且")) {

                msg = msg.replace("恢复并且打开", "");
                SendMsg.send(sender, "小y：调用小爱同学API为您开启" + msg);

            }
            return "";
        }


        if (flag && self.equals("0") && msg.contains("@see")) {
            msg = msg.replace("@see", "");
            if (msg.contains("功能")) {
                SendMsg.send(sender,""+
                        "报时     情话      搜索图片\n" +
                        "新闻     天气      翻译   \n" +
                        "闲聊     笑话      打开网站\n"+
                        "点歌     游戏      百度搜索等" +
                        "更多功能联系wx：longtime991"
                );
                return "";

            } else {
                if (msg.contains("美女图片") || msg.contains("美女照片") || msg.contains("来一张美女") || msg.contains("换一张美女") || msg.contains("换个美女")
                        || msg.contains("来个美女") || msg.contains("看美女") || msg.contains("发个美女")) {
                    String result = HttpRequestUtil.doGet("https://api.kit9.cn/api/random_lady_pictures/api.php?type=json");
                    System.out.println(result);
                    JSONObject jsonObject = JSON.parseObject(result);
                    String data = jsonObject.getString("data");
                    jsonObject = JSON.parseObject(data);
                    data = jsonObject.getString("image_link");
                    String s = DownloadFile.saveImg(data);
                    SendMsg.sendImg(sender, s);


                } else if (msg.contains("二次元图片") || msg.contains("二次元照片") || msg.contains("来一张二次元") || msg.contains("换一张二次元") || msg.contains("换个二次元")
                        || msg.contains("来个二次元") || msg.contains("发个二次元")) {
                    String result = HttpRequestUtil.doGet("https://api.vvhan.com/api/acgimg?type=json");
                    System.out.println(result);
                    JSONObject jsonObject = JSON.parseObject(result);
                    String data = jsonObject.getString("imgurl");
//                        jsonObject = JSON.parseObject(data);
//                        data = jsonObject.getString("image_link");
//                        String s = DownloadFile.saveImg(data);
                    String s = DownloadFile.saveImg(data);

                    SendMsg.sendImg(sender, s);


                } else if (msg.contains("点歌")) {
                    msg = msg.replace("点歌", "");

                    try {
                        HashMap aiMsg = SongUtil.getAiMsg(msg);
                        SendMsg.send(sender, String.valueOf(aiMsg.get("id")));
                        Thread.sleep(60);
                        SendMsg.send(sender, "为您带来" + String.valueOf(aiMsg.get("name")));
//                            Thread.sleep(60);
//                            String s = DownloadFile.saveImg(String.valueOf(aiMsg.get("img1v1Url")));

//                            SendMsg.sendImg(sender,s);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                } else if (msg.contains("打开网站")) {
                    msg = msg.replace("打开网站", "");

                    String s = null;
                    try {
                        s = CutPicture.openUrl(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    SendMsg.sendImg(sender, s);


                } else if (msg.contains("打开油管")) {

                    String s = null;
                    try {
                        s = CutPicture.openYGUrl(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    SendMsg.sendImg(sender, s);


                } else if (msg.contains("打开P站")) {
                    SendMsg.send(sender, "请做和谐市民");

//                        String s = null;
//                        try {
//                            s = CutPicture.openPUrl(msg);
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        SendMsg.sendImg(sender,s);


                } else if (msg.contains("搜索")) {
                    msg = msg.replace("百度搜索", "");

                    String s = null;
                    try {
                        s = CutPicture.openBdUrl(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    SendMsg.sendImg(sender, s);


                } else {
                    String translate = null;
                    try {
                        if (msg.contains("新闻")) {
                            translate = DemoTianAPI.getNews();

                        } else if (msg.contains("翻译")) {
                            msg = msg.replace("翻译", "");
                            translate = TranslateUtil.translate(msg);

                        } else if (msg.contains("天气")) {
                            msg = msg.replace("天气", "");
                            translate = WeatherUtil.getWeather(msg);

                        } else if (msg.contains("情话")) {
                            translate = LoverWordUtil.getLoverWord();

                        } else if (msg.contains("笑话")) {
                            translate = DemoTianAPI.getJoker();

                        } else if (msg.contains("报时")) {
                            translate = "北京时间" + DateUtil.now() + "\n" + "今日" + WeatherUtil.getWeather("武汉") + "\n每日一言\n" + LoverWordUtil.getLoverWord();

                        } else {
                            translate = AiChatUtil.getAiMsg(msg);
                        }
                    } catch (Exception e) {
                        translate = "服务故障，请联系微信 longtime991";
                    }
//                    SendMsg.send3(sender,   translate ,roomsender);

                    SendMsg.send(sender, translate);

                    return translate;
                }
            }


        }


        return "";


    }
}
